<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <script>
    /* 
      异步请求().then(function (data) {
        data就是请求的结果
        return 值 // 这个值不是promise对象
      }).then(function (data) {
        // 这里的data就是上一个then中函数的返回值
      })
    
    
     */
    /* function get (url, success) {
      const xhr = new XMLHttpRequest()
      xhr.onreadystatechange = function () {
        if (xhr.status === 200 && xhr.readyState === 4) {
          success && success(xhr.responseText)
        }
      }
      xhr.open('GET', url, true)
      xhr.send()
    } */

    function get (url) {
      return new Promise (function (resolve, reject) {
        const xhr = new XMLHttpRequest()
        xhr.onreadystatechange = function () {
          if (xhr.status === 200 && xhr.readyState === 4) {
            resolve(xhr.responseText)
          }
          if (xhr.status >= 400 && xhr.readyState === 4) {
            reject(xhr.statusText)
          }
        }
        xhr.open("GET", url, true)
        xhr.send()
      })
    }

    get('http://localhost/ajax/CORS.php?a=1').then(function (data) {
      console.log(data)
      return 110
    }).then(function (data) {
      console.log(data)
    }).catch(function (err) {
      console.log(err)
    })
    
  
  </script>
</body>
</html>